package com.delaphone.flocash.web.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author tamdx
 */
public class Validate {

    /**
     * Check for e-mail validity
     *
     * @param string $email e-mail address to validate
     * @return boolean Validity is ok or not
     */
    public static boolean isEmail(String email) {
        if (email == null || email.equals("")) {
            return false;
        }
        
        String regex = "^([a-zA-Z0-9_-]+\\.)+([a-zA-Z0-9_-])+\\@(([a-zA-Z0-9-])+\\.)+([a-zA-Z]{2,4})$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(email);
        String regex1 = "^([a-zA-Z0-9_-])+\\@(([a-zA-Z0-9-])+\\.)+([a-zA-Z]{2,4})$";
        Pattern p1 = Pattern.compile(regex1);
        Matcher m1 = p1.matcher(email);
        if (m.matches() || m1.matches()) {
            return true;
        } else {
            return false;
        }
    }
    
    public static boolean isPhoneNumber(String phone) {
        return isNumber(phone);
    }
    
    public static boolean isNumber(String number) {
        String regex = "([0-9]*)";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(number);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * check password validity
     *
     * @param plaintext
     * @return boolean Validity is ok or not
     */
    public static boolean isPassword(String plaintext) {
        if (isEmptyString(plaintext)) {
            return false;
        }
        int length = plaintext.length();
        if (length < 6 || length > 8) {
            return false;
        }
        return true;
    }

    /**
     * Check if a string is null or empty
     *
     * @param s
     *            The string input
     * @return true if string input is null or empty, else return false
     */
    public static boolean isEmptyString(String s) {
        if (s == null) {
            return true;
        }
        s = s.trim();
        if (s.equals("")) {
            return true;
        }
        return false;
    }
}
